

/*********************************辅助类util*************************************/
//辅助类 Util
const util = {};

util.type = function (obj) {
	return Object.prototype.toString.call(obj).replace(/\[object\s|\]/g, "");
};

util.isArray = function (list) {
	return util.type(list) === "Array";
};
util.isBoolean = function (o) {
	return util.type(o) == "Boolean";
};
util.isString = function (list) {
	return util.type(list) == "String";
};
util.isFunction = function (func) {
	return util.type(func) == "Function";
};
util.isNumber = function (func) {
	return util.type(func) == "Number";
};
util.isUndefined = function (o) {
	return util.type(o) == "Undefined";
};

util.each = function (array, fn) {
	for (var i = 0, len = array.length; i < len; i++) {
		fn(array[i], i);
	}
};
util.isObject = function (obj) {
	return obj && util.type(obj) == "Object";
};

util.toArray = function (listLike) {
	if (!listLike) {
		return [];
	}
	var list = [];
	for (var i = 0, len = listLike.length; i < len; i++) {
		list.push(listLike[i]);
	}
	return list;
};

util.setAttr = function (node, key, value) {
	switch (key) {
		case "style":
			node.style.cssText = value;
			break;
		case "value":
			var tagName = node.tagName || "";
			tagName = tagName.toLowerCase();
			if (tagName === "input" || tagName === "textarea") {
				node.value = value;
			} else {
				node.setAttribute(key, value);
			}
			break;
		case "checked":
			var tagName = node.tagName || "";
			tagName = tagName.toLowerCase();
			if (/^(input||option)$/.test(tagName)) {
				node.checked = value;
			} else {
				node.setAttribute(key, value);
			}
            break;
        case "selected":
			var tagName = node.tagName || "";
			tagName = tagName.toLowerCase();
			if (/^(input||option)$/.test(tagName)) {
				node.selected = value;
			} else {
				node.setAttribute(key, value);
			}
			break;
		default:
			node.setAttribute(key, value);
			break;
	}
};

util.toString = function (obj) {
	if (util.isArray(obj)) {
		var tempStr = "[";
		for (var index = 0; index < obj.length; index++) {
			var element = obj[index];
			tempStr += element + ",";
		}
		tempStr = tempStr.replace(/\,$/, "") + "]";
		return tempStr;
	} else if (util.type(obj) == "Object") {
		return JSON.stringify(obj);
	} else if (util.isNumber(obj)) {
		return obj + "";
	} else if (util.isString(obj)) {
		return obj;
	} else if (util.isBoolean(obj)) {
		return obj.toString();
	} else if (util.isUndefined(obj)) {
		return "";
	}
	return "";
};

util.uuid = function (len, radix) {
	var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".split("");
	var uuid = [],
		i;
	radix = radix || chars.length;

	if (len) {
		// Compact form
		for (i = 0; i < len; i++) uuid[i] = chars[0 | (Math.random() * radix)];
	} else {
		// rfc4122, version 4 form
		var r;

		// rfc4122 requires these characters
		uuid[14] = "4";
		// Fill in random data. At i==19 set the high bits of clock sequence as
		// per rfc4122, sec. 4.1.5
		for (i = 0; i < 32; i++) {
			if (!uuid[i]) {
				r = 0 | (Math.random() * 16);
				uuid[i] = chars[i == 19 ? (r & 0x3) | 0x8 : r];
			}
		}
	}

	return uuid.join("");
};
/**
 * 对象深拷贝
 */
util.copy = function (obj) {
	var newObj = JSON.parse(JSON.stringify(obj));

	return newObj;
};
util.copyArr = function (array) {
	var nArr = [];
	for (var index = 0; index < array.length; index++) {
		array[index] && nArr.push(array[index]);
	}
	return nArr;
};

/* 部分隐藏处理
 ** str 需要处理的字符串
 ** frontLen  保留的前几位
 ** endLen  保留的后几位
 ** cha  替换的字符串
 */
util.plusXing = function (str, startlength, flength, cha) {
	return str.substring(0, startlength) + cha + str.substring(startlength + flength);
};

util.getMathCharacter = function (content) {
	// content
	var leftCharNum = 0;
	var arr = [];
	var leftPostion = -1;
	var nowPosition = 0;
	while (nowPosition < content.length) {
		//如果沒有遍歷完
		if (content[nowPosition] == "{" && content[nowPosition + 1] == "{" && content[nowPosition + 2] != "{") {
			//如果找到了第一個匹配符
			leftCharNum++; //標志位加1
			if (leftPostion == -1) {
				//如果没有记录了位置
				leftPostion = nowPosition;
			}
		} else if (content[nowPosition] == "}" && content[nowPosition + 1] == "}") {
			//如果找到右边匹配符
			leftCharNum--; //标志位-1
			if (leftCharNum == 0) {
				//如果已经是匹配符号
				arr.push({
					text: content.substring(leftPostion + 2, nowPosition),
					left: leftPostion,
					len: nowPosition - leftPostion + 2
				}); //添加结果集
				leftPostion = -1; //左边标志位重新恢复
			}
		}
		nowPosition++;
	}

	return arr;
};

/**
 * 移除前后的单双引号
 */
util.removeSideSymbol = function (str) {
	return str.replace(/(^\')/g, "").replace(/(\'$)/g, "").replace(/(^\")/g, "").replace(/(\")/g, "")
};

util.debounce = function (func, wait, immediate) {
	// immediate默认为false
	var timeout, args, context, timestamp, result;

	var later = function () {
		// 当wait指定的时间间隔期间多次调用_.debounce返回的函数，则会不断更新timestamp的值，导致last < wait && last >= 0一直为true，从而不断启动新的计时器延时执行func
		var last = new Date().getTime() - timestamp;

		if (last < wait && last >= 0) {
			timeout = setTimeout(later, wait - last);
		} else {
			timeout = null;
			if (!immediate) {
				result = func.apply(context, args);
				if (!timeout) context = args = null;
			}
		}
	};

	return function () {
		context = this;
		args = arguments;
		timestamp = new Date().getTime();
		// 第一次调用该方法时，且immediate为true，则调用func函数
		var callNow = immediate && !timeout;
		// 在wait指定的时间间隔内首次调用该方法，则启动计时器定时调用func函数
		if (!timeout) timeout = setTimeout(later, wait);
		if (callNow) {
			result = func.apply(context, args);
			context = args = null;
		}

		return result;
	};
};

// util.getTeplate=function(url) {
// 	return new Promise((resolve,reject)=>{
// 		var request;
// 		if (window.XMLHttpRequest) {
// 			request = new XMLHttpRequest();//W3C
// 		} else {
// 			request = new ActiveXObject('MicroSoft.XMLHTTP');//IE
// 		}
// 		request.open('get', url);
// 		request.onreadystatechange = function () {
// 			if (request.readyState == 4 && request.status == 200) {
// 				resolve(request.responseText);
// 			}
// 			if (request.readyState == 4 &&request.status == 404) {
// 				reject(request.status+' : '+request.statusText);
// 			}
// 		};
// 		request.send();
// 	});
// }
module.exports = util;
